<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- 经销商选择 -->
 <div class="content-show" id="searchDivb" style="display: none;">
  	<div class="searchtop">
		<input type="text"  class="form-control smallicon fixedsearch" id="searchdealerName" name="searchdealerName" placeholder="经销商名称" onkeyup="businesscho(this.value, true);" onafterpaste="businesscho(this.value, true);">
	</div>
	<ul class="cho-ul" id="searchUlb"></ul>
</div>

<!-- 经销商选择-->
<script type="text/javascript">
var exeteb = true;
function businesscho(name,hasOpen){
   if(exeteb){
   	exeteb = false;
   	setTimeout(function(){
   		exeteb = true;
		}, 500);
   	business(name,hasOpen);
   } else {
	    setTimeout(function(){
   		if(exeteb) { //超过500ms还没有触发，则再补充执行最后一次
   			business(name, true);
   		}
		}, 500);
   }
 }

function removeDuan(){
	 var minZindex = 0;
		var $shadeDiv;
		$('.layui-layer-shade').each(function(i,obj){
			var thisIndex = $(this).css('z-index');
			if(i == 0) {
				minZindex = thisIndex;
				$shadeDiv = $(this);
			} else {
				if(minZindex > thisIndex) {
					$shadeDiv.remove();
					$shadeDiv = $(this);
					minZindex = thisIndex;
				} else {
					$(this).remove();
				}
			}
		});
		$('.layui-layer-move').each(function(i,obj){
			$(this).remove();
		});
}

function business(name,hasOpen){
	var name = name || '';
		$.dpAjax({
			show:false,
			url:'${basePath}/DealerInfoController/list',
			data:{dealerName:name,rows:8},
			success: function(datas){
			 	  if(datas.flag == "100100")
				  {
			 		var records = datas.result.rows;
			 		$searchUlb = $('#searchUlb');
			 		$searchUlb.empty();
			 		if(records && records.length > 0) {
			 			var content = '';
				 		for(var i=0;i< records.length; i++){
				 			var record = records[i];
			 				content +='<a href="javascript:choiceb(\''+record.replaceId+'\', \''+record.dealerName+'\');"><li><span class="li-busname"><lable>名称:</lable>' + record.dealerName +'</span><lable>所在城市:</lable>' + record.dealerCityDisplay +'</li></a>';  
				 		}
				 		$searchUlb.html(content);
			 		}
			 		if(!hasOpen) {
			 			$("#searchdealerName").val(name);
						layer.open({
						  title:'经销商选择',
						  type: 1,
						  area: ['600px', '430px'], //宽高
						  content: $('#searchDivb')
						}); 
			 		}
			 		removeDuan();
			 		$("#searchdealerName").focus();
					return;
				  }
				  if(datas.flag == "100200")
              {
					  alert(JSON.stringify(datas));
            	  return;
              }
				  layer.msg(datas.message, {icon: 2});
         }
	 	});
		
	}
		

	var choiceb = function(id,name)
	{
		document.getElementById("p-business-cho").value = name;
		document.getElementById("business-cho").value = id;
		layer.closeAll(); 
	}
</script>